/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * topUp.java
 *
 * Created on 29-Sep-2010, 15:59:45
 */

package COMIC_BOOK_STORE;

import java.sql.*;
import javax.swing.JOptionPane;

/**
 *
 * @author benson
 */

public class ACCOUNT_TOPUP extends javax.swing.JFrame {

    /** Creates new form topUp */
    public ACCOUNT_TOPUP() {
        super("TOPUP ACCOUNT");
        initComponents();
        this.setLocationRelativeTo(null);
        topUpPanel.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        topUpAmount = new javax.swing.ButtonGroup();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jSeparator1 = new javax.swing.JSeparator();
        memberCodeLabel = new javax.swing.JLabel();
        memberCodeTextField = new javax.swing.JTextField();
        topUpPanel = new javax.swing.JPanel();
        displayIcNumber = new javax.swing.JLabel();
        displayCustomerName = new javax.swing.JLabel();
        displayCreditBalance = new javax.swing.JLabel();
        RM150RadioButton = new javax.swing.JRadioButton();
        RM100RadioButton = new javax.swing.JRadioButton();
        RM50RadioButton = new javax.swing.JRadioButton();
        RM30RadioButton = new javax.swing.JRadioButton();
        topupAmountLabel = new javax.swing.JLabel();
        creditBalanceLabel = new javax.swing.JLabel();
        icNumberLabel = new javax.swing.JLabel();
        customerNameLabel = new javax.swing.JLabel();
        backButton = new javax.swing.JButton();
        enterButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jSeparator1.setBounds(10, 120, 890, 10);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        memberCodeLabel.setText("Member Code");
        memberCodeLabel.setBounds(20, 40, 90, 30);
        jLayeredPane1.add(memberCodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        memberCodeTextField.setBounds(210, 40, 170, 30);
        jLayeredPane1.add(memberCodeTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayIcNumber.setText("ic no here");

        displayCustomerName.setText("name here");

        displayCreditBalance.setText("credit balance here");

        topUpAmount.add(RM150RadioButton);
        RM150RadioButton.setText(" RM 150");

        topUpAmount.add(RM100RadioButton);
        RM100RadioButton.setText(" RM 100");

        topUpAmount.add(RM50RadioButton);
        RM50RadioButton.setText(" RM 50");

        topUpAmount.add(RM30RadioButton);
        RM30RadioButton.setText(" RM 30");

        topupAmountLabel.setText("TopUp Amount");

        creditBalanceLabel.setText("Credit Balance");

        icNumberLabel.setText("IC no.");

        customerNameLabel.setText("Name");

        backButton.setText("CANCEL");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        enterButton.setText("TOP UP");
        enterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enterButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout topUpPanelLayout = new javax.swing.GroupLayout(topUpPanel);
        topUpPanel.setLayout(topUpPanelLayout);
        topUpPanelLayout.setHorizontalGroup(
            topUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(topUpPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(topUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(customerNameLabel)
                    .addComponent(icNumberLabel)
                    .addComponent(creditBalanceLabel))
                .addGap(39, 39, 39)
                .addGroup(topUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(displayCustomerName, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(displayIcNumber)
                    .addComponent(displayCreditBalance))
                .addGap(18, 18, 18)
                .addGroup(topUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(topUpPanelLayout.createSequentialGroup()
                        .addGap(229, 229, 229)
                        .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(enterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(RM150RadioButton)
                    .addComponent(RM50RadioButton)
                    .addComponent(RM30RadioButton)
                    .addComponent(RM100RadioButton)
                    .addComponent(topupAmountLabel))
                .addGap(37, 37, 37))
        );

        topUpPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {displayCreditBalance, displayCustomerName, displayIcNumber});

        topUpPanelLayout.setVerticalGroup(
            topUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(topUpPanelLayout.createSequentialGroup()
                .addGroup(topUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(topUpPanelLayout.createSequentialGroup()
                        .addComponent(displayCustomerName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(13, 13, 13)
                        .addGroup(topUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(icNumberLabel)
                            .addComponent(displayIcNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(topUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(creditBalanceLabel)
                            .addComponent(displayCreditBalance))
                        .addGap(42, 42, 42)
                        .addGroup(topUpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(enterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(topUpPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(customerNameLabel))
                    .addGroup(topUpPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(topupAmountLabel)
                        .addGap(18, 18, 18)
                        .addComponent(RM30RadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RM50RadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RM100RadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RM150RadioButton)))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        topUpPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {displayCreditBalance, displayCustomerName, displayIcNumber});

        topUpPanel.setBounds(10, 140, 770, 240);
        jLayeredPane1.add(topUpPanel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setText("O K ");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(530, 70, 110, 40);
        jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton2.setText("BACK");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.setBounds(650, 70, 100, 40);
        jLayeredPane1.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/Background3 copy.jpg"))); // NOI18N
        jLabel1.setText("jLabel1");
        jLabel1.setBounds(0, 0, 900, 500);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        String query = "Select * from MEMBER where MEMBER_CODE = '" + memberCodeTextField.getText() + "'";

//      --------------------------------------------------------------------------------------------------

                    try
            {
                DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");;

//      --------------------------------------------------------------------------------------------------

                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);

//      --------------------------------------------------------------------------------------------------

                   while (rs.next())
                   {
                 id = rs.getString(1).trim();
                 displayCustomerName.setText(rs.getString(2).trim());
                 displayIcNumber.setText(rs.getString(7));
                 displayCreditBalance.setText(rs.getString(14));
                   }
                
            System.out.println("OK!");
            }//end try

            catch (Exception e)
            {System.out.println("error occurred");}

//      --------------------------------------------------------------------------------------------------

        if (memberCodeTextField.getText().equals(id))
        {
            topUpPanel.setVisible(true);
            creditAmount = Float.valueOf(displayCreditBalance.getText()).floatValue();
        }

        else
        {
            topUpPanel.setVisible(false);
            JOptionPane.showMessageDialog(null, "Invalid input entered. \nPlease try again.", "ERROR!", JOptionPane.WARNING_MESSAGE);
        }


    }                                        

    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            

        int reloadAmount = 0;

        if (RM30RadioButton.isSelected())
            reloadAmount = 30;
        else if (RM50RadioButton.isSelected())
            reloadAmount = 50;
        else if (RM100RadioButton.isSelected())
            reloadAmount = 100;
        else if (RM150RadioButton.isSelected())
            reloadAmount = 150;
        else JOptionPane.showMessageDialog(null, "Please select amount of reload.", "ERROR!", JOptionPane.WARNING_MESSAGE);

        if (reloadAmount != 0)
        {
            creditAmount += reloadAmount;

//      --------------------------------------------------------------------------------------------------

                    try
            {
                DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");;

//      --------------------------------------------------------------------------------------------------

                PreparedStatement pstmt1 = con.prepareStatement("update MEMBER "
                        + "set MEMBER_CREDIT = '" + creditAmount + "'"
                        + "where MEMBER_CODE = '" + id + "'");

//      --------------------------------------------------------------------------------------------------

            pstmt1.executeUpdate();
            pstmt1.close();

            System.out.println("OK! statement entered");
            JOptionPane.showMessageDialog(null, "Top up successful.", "System Message", JOptionPane.INFORMATION_MESSAGE);
            topUpPanel.setVisible(false);
            
            }//end try

            catch (Exception e)
            {System.out.println("error occurs during update credit");}

//      --------------------------------------------------------------------------------------------------

        }//end if


    }                                           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.dispose();
        new MAIN_MENU().setVisible(true);
    }                                        

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        topUpPanel.setVisible(false);
    }                                          

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ACCOUNT_TOPUP().setVisible(true);
            }
        });
    }

    public float creditAmount = 0;
        String id = "000";

    // Variables declaration - do not modify                     
    private javax.swing.JRadioButton RM100RadioButton;
    private javax.swing.JRadioButton RM150RadioButton;
    private javax.swing.JRadioButton RM30RadioButton;
    private javax.swing.JRadioButton RM50RadioButton;
    private javax.swing.JButton backButton;
    private javax.swing.JLabel creditBalanceLabel;
    private javax.swing.JLabel customerNameLabel;
    private javax.swing.JLabel displayCreditBalance;
    private javax.swing.JLabel displayCustomerName;
    private javax.swing.JLabel displayIcNumber;
    private javax.swing.JButton enterButton;
    private javax.swing.JLabel icNumberLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel memberCodeLabel;
    private javax.swing.JTextField memberCodeTextField;
    private javax.swing.ButtonGroup topUpAmount;
    private javax.swing.JPanel topUpPanel;
    private javax.swing.JLabel topupAmountLabel;
    // End of variables declaration                   

}
